package com.cash.lemoloan.statisticlib.Utils;


import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.database.Cursor;
import android.provider.CallLog;

import com.cash.lemoloan.statisticlib.StatisticApplication;
import com.cash.lemoloan.statisticlib.model.StatisticCallInfo;

import java.util.ArrayList;
import java.util.List;

//获取通话记录
public class CallUtil {

    private static CallUtil callUtil;

    public static CallUtil getInstance() {
        if (callUtil == null) {
            callUtil = new CallUtil();
        }
        return callUtil;
    }

    public synchronized static List<StatisticCallInfo> getCallInfo() {
        List<StatisticCallInfo> callLogsInfo = new ArrayList<>();
        Cursor cursor = null;
        try {
            cursor = StatisticApplication.getInstance()
                    .getContentResolver().query(CallLog.Calls.CONTENT_URI, // 查询通话记录的URI
                            new String[]{CallLog.Calls.CACHED_NAME// 通话记录的联系人
                                    , CallLog.Calls.NUMBER// 通话记录的电话号码
                                    , CallLog.Calls.DATE// 通话记录的日期
                                    , CallLog.Calls.DURATION// 通话时长
                                    , CallLog.Calls.TYPE}// 通话类型
                            , null, null, CallLog.Calls.DEFAULT_SORT_ORDER// 按照时间逆序排列，最近打的最先显示
                    );
            if (cursor == null) {
                return null;
            }
            while (cursor.moveToNext()) {
                @SuppressLint("Range") String name = cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME));
                @SuppressLint("Range") String number = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
                @SuppressLint("Range") long date = cursor.getLong(cursor.getColumnIndex(CallLog.Calls.DATE));
                @SuppressLint("Range") int duration = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.DURATION));
                @SuppressLint("Range") int type = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.TYPE));
                StatisticCallInfo callLogInfo = new StatisticCallInfo();
                callLogInfo.setStatisticCallName(name);
                callLogInfo.setStatisticCallData(String.valueOf(date));
                callLogInfo.setStatisticCallNumber(number);
                callLogInfo.setStatisticCallDuration(String.valueOf(duration));
                callLogInfo.setStatisticCallType(String.valueOf(type));
                callLogsInfo.add(callLogInfo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                try {
                    cursor.close();
                } catch (Exception e) {
                }
            }
            return callLogsInfo;
        }
    }

    public int getCallLogCount() {
        ContentResolver resolver = StatisticApplication.getInstance().getContentResolver();
        if (resolver == null) {
            return -1;
        }
        Cursor cursor = resolver.query(CallLog.Calls.CONTENT_URI, // 查询通话记录的URI
                new String[]{CallLog.Calls.CACHED_NAME// 通话记录的联系人
                        , CallLog.Calls.NUMBER// 通话记录的电话号码
                        , CallLog.Calls.DATE// 通话记录的日期
                        , CallLog.Calls.DURATION// 通话时长
                        , CallLog.Calls.TYPE}// 通话类型
                , null, null, CallLog.Calls.DEFAULT_SORT_ORDER);// 按照时间逆序排列，最近打的最先显示
        int count = (cursor == null ? -1 : cursor.getCount());
        if (cursor != null) {
            cursor.close();
        }
        return count;
    }
}
